import { reactive, onMounted, ref, nextTick } from 'vue'
import useInstance from '@/hooks/useInstance';
import { EditType } from '@/type/BaseEnum'
import { AddTestCaseApi,testCaseDelApi } from '@/api/test/test'
export default function addTestCase(getTestCaseData) {
  const { global, proxy } = useInstance()
  const RefCaseAddEdit = ref<{ show: (type: string, row?: any) => void }>()
  const childId = ref('')
  const addBtn = (row) => {
    console.log(row);
    childId.value = row.id
    RefCaseAddEdit?.value?.show(EditType.ADD)
  }
  const editBtn = (row) => {
    RefCaseAddEdit?.value?.show(EditType.EDIT, row)
  }
  const save = async (row) => {
    // console.log(row, 'save');
    let res
    if (row?.id) {
      // res = await roleUpdateApi(row)
    } else {
      res = await AddTestCaseApi(row)
    }
    if (res.code === 200 && row?.id) {
      global.$message({ message: '编辑成功', type: 'success' })
    } else {
      global.$message({ message: '新增成功', type: 'success' })      
      getTestCaseData()
    }
  }
  const deleteBtns = async (id: number | string) => {
    const confirm = await global.$myconfirm('确定删除该测试用例嘛?')
    if (confirm) {
      const res = await testCaseDelApi({ id })
      if (res.code === 200) {
        global.$message({ message: '删除成功', type: 'success' })
        getTestCaseData()
      }
    } else {
      global.$message({ message: "已取消删除", type: 'warning' })
    }
  }
  return {
    addBtn, editBtn, save, deleteBtns, RefCaseAddEdit,childId
  }
}